package com.nowcoder.community.controller;

import com.nowcoder.community.pojo.DiscussPost;
import com.nowcoder.community.pojo.Page;
import com.nowcoder.community.pojo.User;
import com.nowcoder.community.service.DiscussPostService;
import com.nowcoder.community.service.LikeService;
import com.nowcoder.community.service.UserService;
import com.nowcoder.community.util.CommunityConstant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(description = "首页")
@Controller
public class HomeController implements CommunityConstant {

    @Autowired
    @Qualifier("discussPostService")
    private DiscussPostService discussPostService;

    @Autowired
    @Qualifier("userService")
    private UserService userService;

    @Autowired
    @Qualifier("likeService")
    private LikeService likeService;

    @GetMapping("/")
    public String root(){
        return "forward:/index";
    }



    @ApiOperation(value = "分页显示帖子")
    @GetMapping("/index")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "page", value = "分页实例"),
        @ApiImplicitParam(name = "orderMode", value = "帖子显示的方式")
    })
    public String getIndexPage(Model model, Page page,
                               @RequestParam(name = "orderMode", defaultValue = "0") int orderMode){
        //SpringMVC中会实例化请求方法中的接收参数Model model和Page page，并且会自动将page注入到model中

        //设置页面中的帖子总数
        page.setRows(discussPostService.selectDiscussPostRows(0));

        //设置页面的路径
        page.setPath("/index?orderMode=" + orderMode);

        //在查找每页的帖子时，起始行位置不包括page.getOffset()（sql中使用了LIMIT）
        // userId为0时表明查询时没有了userId这个字段的限制
        List<DiscussPost> list = discussPostService.selectDiscussPosts(0,
                page.getOffset(), page.getLimit(), orderMode);

        //将用户和对应用户的帖子一一对应并存放到容器中
        List<Map<String, Object>> discussPosts = new ArrayList<>();
        if (list != null){
            for (DiscussPost post : list) {
                //这里最好是在内部创建容器，因为JDK1.8的HashMap的key的hash值的散列性并不是很高
                Map<String, Object> map = new HashMap<>();
                map.put("post", post);
//                System.out.println("---->" + post.getId());
                User user = userService.findUserById(post.getUserId());
                /***************************************************************/
                map.put("user", user);
//                System.out.println("---->" + user);

                //查询帖子被点赞的数量
                long likeCount = likeService.findEntityLikeCount(ENTITY_TYPE_POST, post.getId());
                map.put("likeCount", likeCount);

                discussPosts.add(map);//每个map中包含一个用户和一个帖子
            }
        }
        model.addAttribute("discussPosts", discussPosts);
        model.addAttribute("page", page);
        model.addAttribute("orderMode", orderMode);
        return "index";
    }

    @GetMapping("/error")
    public String getErrorPage(){
        return "error/500";
    }

    @GetMapping("/denied")
    public String getDeniedPage(){
        return "error/404";
    }
}
